package net.jlibrary.curbparking.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.curbparking.action.vo.BerthVo;
import net.jlibrary.curbparking.domain.Device;
import net.jlibrary.curbparking.service.IBerthManager;

@RequestMapping("/berth")
@RestController
public class BerthAdminAction {

	@Autowired
	private IBerthManager berthManager;

	@RequestMapping(value = "/berthAdminView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/curbparking/berthAdminView");
	}

	@RequestMapping(value = "/berthAdminViewList", method = { RequestMethod.POST })
	public Pagination lists(BerthVo berthVo, PaginationParam param) {
		Pagination pagination = berthManager.listsPage(berthVo, param);
		return pagination;
	}

	@RequestMapping(value = "/berthFormInit", method = { RequestMethod.GET })
	public BerthVo addFormInit() {
		BerthVo berthVo = new BerthVo();
		return berthVo;
	}

	@RequestMapping(value = "/detailBerth", method = { RequestMethod.GET })
	public BerthVo detail(BerthVo berthVo) {
		berthVo = berthManager.get(berthVo);
		return berthVo;
	}

	@RequestMapping(value = "/saveBerth", method = { RequestMethod.POST })
	@Token
	public ModelMap save(@ModelAttribute("vm") BerthVo berthVo, @Token String token, BindingResult result) {
		berthManager.save(berthVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/updateBerth", method = { RequestMethod.POST })
	@Token
	public ModelMap update(@ModelAttribute("vm") BerthVo berthVo, @Token String token, BindingResult result) {
		berthManager.update(berthVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/deleteBerth", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		BerthVo berthVo = new BerthVo();
		berthVo.setId(id);
		berthManager.delete(berthVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/deviceJsonData", method = { RequestMethod.GET })
	public List<ModelMap> deviceJsonData() {
		List<Device> devices = berthManager.devicedata();
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (Device device : devices) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, device.getId());
			map.put("code", device.getCode());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, device.getName());
			maps.add(map);
		}
		return maps;
	}
}
